<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <#assign base=request.contextPath  />
    <script src="${base}/js/jquery-3.2.0.js"></script>
</head>
<body>
<h1>手机注册</h1>
    <div class="container">

            <table>
                <tr>
                    <td>手机号:</td>
                    <td><input type="text" name="username" id="username"></td>
                </tr>
                <tr>
                    <td>手机验证码:</td>
                    <td><input type="text" name="code" id="code"><input type="button" onclick="sendSMS();" value="发送验证码"></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><input type="password" name="userpwd" id="userpwd"></td>
                </tr>
                <tr>
                    <td>确认密码:</td>
                    <td><input type="password" name="userpwd1" id="userpwd1" ><span style="color: red;" id="tishi"></span></td>
                </tr>
                <tr>
                    <td><input type="button" onclick="toReg();" value="提交"></td>
                </tr>
            </table>

    </div>
<script type="text/javascript">
    function isPoneNo(pone) {
        var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
        if (!myreg.test(pone)) {
            return false;
        } else {
            return true;
        }
    }

    function sendSMS(){
        var username = $("#username").val();
        if(!isPoneNo(username)){
            alert("无效的手机号");
            return;
        }
        $.ajax({
            url : "sendCode",
            type : "post",
            data : {
                username : username
            },
            success : function (data) {
                console.log(data);
                var jsdata = $.parseJSON(data);
                if (jsdata.rtncode == 0) {
                    alert("短信验证码发送成功，请注意查收！");
                }else {
                    alert("短信验证码发送失败："+jsdata.errmsg);
                }
            },
            error : function (data) {
                console.log(data);
                alert("短信验证码发送异常："+data);
            }
        });
    }

    function toReg(){
        var username = $("#username").val();
        var pwd1 = $("#userpwd").val();
        console.log(pwd1);
        var pwd2 = $("#userpwd1").val();
        console.log(pwd2);
        if(!isPoneNo(username)){
            alert("无效的手机号");
            return;
        }
        if (pwd1 != pwd2) {
            $("#tishi").html("两次密码不相同");
            return;
        }
        $.ajax({
            url : "user/userReg",
            type : "post",
            data : {
                username : $("#username").val(),
                code : $("#code").val(),
                userpwd : $("#userpwd").val()
            },
            success : function(data){
                console.log(data);

                if(data == 0){
                    alert("注册成功！")
                    window.location.href="login?time=New Date()";
                }else if(data = 3){
                    alert("注册失败：验证码错误")
                }else if(data = 4){
                    alert("注册失败：该手机号已注册，请检查")
                }else {
                    alert("注册失败：填写的信息有误，请检查")
                }

            },
            error : function(data){
                console.log(data);
                alert("注册失败："+data)
            }
        });

    }
</script>
</body>
</html>